package bancosys.tec.security.chinesewall;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Contexto de chinese wall.
 * @author Marco
 */
public class ChineseWallContext implements Serializable {
    private boolean enabled = true;

    private Set<String> disabledFilters = new HashSet<String>();
    private Map<String, ChineseWallFilterProperties> filterPropertiesMap = new HashMap<String, ChineseWallFilterProperties>();

    /**
     * Adds a parameter to the Chinese Wall context.
     * @param filterName filter name
     * @param parameter parameter name
     * @param values parameter values
     */
    public void addParameter(String filterName, String parameter, Collection<? extends Serializable> values) {
        ChineseWallFilterProperties filterProperties = this.filterPropertiesMap.get(filterName);
        if(filterProperties == null) {
            filterProperties = new ChineseWallFilterProperties(filterName);
            this.filterPropertiesMap.put(filterName, filterProperties);
        }
        if (filterProperties.getParameters().containsKey(parameter)) {
            Collection<Serializable> col = (Collection<Serializable>) filterProperties.getParameters().get(parameter);
            col.addAll(values);
        } else if(values == null){
            filterProperties.getParameters().remove(parameter);
        } else {
            filterProperties.getParameters().put(parameter, new ArrayList<Serializable>(values));
        }
    }
    
    /**
     * Returns the Chinese Wall parameter.
     * @param filterName filterName
     * @param parameter parameter
     * @return Collection
     */
    public Collection<Serializable> getParameter(String filterName, String parameter) {
        ChineseWallFilterProperties filterProperties = this.filterPropertiesMap.get(filterName);
        if(filterProperties == null) {
            return null;
        } else {
            return filterProperties.getParameters().get(parameter);
        }
    }
    
    /**
     * Returns true if this filter is enabled (wasn't disabled).
     * @param name name
     * @return boolean
     */
    public boolean isFilterEnabled(String name) {
        return !this.disabledFilters.contains(name);
    }
    
    /**
     * Disables the filter with the given name.
     * @param name filter name
     */
    public void disableFilter(String name) {
        this.disabledFilters.add(name);
    }

    /**
     * Enables the filter with the given name.
     * @param name filter name
     */
    public void enableFilter(String name) {
        this.disabledFilters.remove(name);
    }

    /**
     * Se o filtro de chinese wall está habilitado.
     * @return <code>true</code> caso esteja habilitado, <code>false</code> caso contrário.
     */
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * Habilita o filtro.
     */
    public void enable() {
        this.enabled = true;
    }

    /**
     * Desabilita o filtro.
     */
    public void disable() {
        this.enabled = false;
    }
}
